package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.TopicDataList;
import com.elinkway.infinitemovies.bean.TopicInfo;
import com.elinkway.infinitemovies.ui.fragment.TopicFragment;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.UIs;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

public class TopicHomeListAdapter extends BaseAdapter {
    private final String TAG = TopicFragment.TAG;
    private Context mContext;
    private LayoutInflater inflater;
    private TopicDataList item_data;

    public TopicDataList getItem_data() {
        return item_data;
    }

    private class ListItemHolder {
        ImageView img_topic_cover;
        TextView tv_topic_title;
        RelativeLayout mTopicView;
    }

    public TopicHomeListAdapter(Context context, TopicDataList result) {
        super();
        mContext = context;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        item_data = result;

    }

    @Override
    public int getCount() {
        return item_data.getTopics().size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ListItemHolder item;
        TopicInfo topic = item_data.getTopics().get(position);
        if (convertView == null) {
            LogUtils.e("", "convert VIew");
            convertView = inflater.inflate(R.layout.topic_home_item, null);
            item = new ListItemHolder();
            item.img_topic_cover = (ImageView) convertView
                    .findViewById(R.id.img_topic_home_list_cover);
            item.tv_topic_title = (TextView) convertView
                    .findViewById(R.id.tv_topic_home_list_title);

            item.mTopicView = (RelativeLayout) convertView
                    .findViewById(R.id.topic_view);
            RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rl.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rl.addRule(RelativeLayout.CENTER_HORIZONTAL);

            convertView.setTag(item);
        } else {
            LogUtils.e("", "convert VIew no ");
            item = (ListItemHolder) convertView.getTag();
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) item.img_topic_cover.getLayoutParams();
            params.topMargin = 0;
            params.bottomMargin = -UIs.dipToPx(20);
            item.img_topic_cover.setLayoutParams(params);
        }
        LogUtils.e(TAG, "get view pos is " + position);

        item.mTopicView.setVisibility(View.VISIBLE);
        ImageLoader.getInstance().displayImage(topic.getPic(),
                item.img_topic_cover);
        item.tv_topic_title.setText(topic.getName());
        return convertView;
    }

    public void addItem_data(TopicDataList topicDatas) {
        if (item_data != null) {
            item_data.getTopics().addAll(topicDatas.getTopics());
        } else {
            item_data = topicDatas;
        }
    }

    public void setItem_data(TopicDataList topicDatas) {
        this.item_data = topicDatas;
    }

}
